//
//  ZZCRegularUbiController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCRegularUbiController.h"
#import "ZZCRegularDetailsController.h"
#import "ZZCLookPayPasswordController.h"
#import "ZZCOwnPropertyController.h"
#import "ZZCMeUbiViewController.h"

@interface ZZCRegularUbiController ()
@property (nonatomic, strong) GRBkeyTextField * safeTextField;
@property (nonatomic, strong) GRBkeyTextField * safeInputTextField;
@property (weak,   nonatomic) IBOutlet UIView *existView;
@property (weak,   nonatomic) IBOutlet UIButton *existBtn;
@property (weak,   nonatomic) IBOutlet UILabel *rateLabel;
@property (strong, nonatomic) ZZCMenuView *menuView;
@property (weak,   nonatomic) IBOutlet UITextField *UBCountTextFld;
@property (copy,   nonatomic) NSString *count;
@property (nonatomic, strong) GRBsafeKeyBoard * board;
@property (copy,   nonatomic) NSString *payPwd;

@property (strong, nonatomic)ZZCRegularDetailsController *reg;

@property (retain, nonatomic) NSArray *array;
@property (retain, nonatomic) NSMutableArray *cycleArr;
@property (retain, nonatomic) NSMutableArray *rateArr;
@property (retain, nonatomic) NSMutableArray *idArr;
@property (assign, nonatomic) float iid;


@end

@implementation ZZCRegularUbiController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"U币转定期";
    [self requestData];
    _cycleArr = [NSMutableArray new];
    _rateArr = [NSMutableArray new];
    _idArr = [NSMutableArray new];

    IQKeyboardManager *iq = [IQKeyboardManager sharedManager];
    iq.enableAutoToolbar = NO;
    _reg = [ZZCRegularDetailsController new];
}
- (void)requestData {
    //    NSString * = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"type":@(2)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"type":@(2)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_RATE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"成功-%@",responseObject);
            _array = responseObject[@"data"][@"list"];
            for (NSDictionary *dict in _array) {
                [_cycleArr addObject:[NSString stringWithFormat:@"%@个月",dict[@"cycle"]]];
                [_idArr addObject:dict[@"id"]];
                [_rateArr addObject:dict[@"rate"]];
            }
        }
    }];
}

//确认
- (IBAction)confirmClick {
    
    self.board = [GRBsafeKeyBoard GRB_showSafeInputKeyBoard];
    __weak typeof(self)weakSelf = self;
    self.board.GRBsafeKeyFinish=^(NSString * passWord){
        if (passWord.length==6) {
            _payPwd = passWord;
            [weakSelf.board GRB_endKeyBoard];
            ZZCLog(@"你输入的密码为---%@",passWord);
            [weakSelf finishAction];
            //在这里判断密码是否正确
            if (YES) {
                ZZCLog(@"你输入的密码正确");
            }else {
                ZZCLog(@"你输入的密码错误");
            }
        }
        
    };
    self.board.GRBsafeKeyClose=^{
        
        [weakSelf.board GRB_endKeyBoard];
    };
    
    self.board.GRBsafeKeyForgetPassWord=^{
        ZZCLog(@"点击忘记密码了");
        ZZCLookPayPasswordController *VC = [ZZCLookPayPasswordController new];
        [weakSelf.navigationController pushViewController:VC animated:YES];

    };
}
- (void)finishAction {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{
                           @"token":token,
                           @"money":self.UBCountTextFld.text,
                           @"rate_id":@(self.iid),
                           @"pay_password":self.payPwd
                           };
    NSLog(@"%@",dict);
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"token":token,
                            @"money":self.UBCountTextFld.text,
                            @"rate_id":@(self.iid),
                            @"pay_password":self.payPwd
                            };
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_TURNREGULAR parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-*************%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            _reg.UbiCountLabel.text = [NSString stringWithFormat:@"%.2fU币",[responseObject[@"data"][@"money"] floatValue]];
            if ([responseObject[@"code"] isEqual:@(200)]) {
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCOwnPropertyController class]]||
                        [controller isKindOfClass:[ZZCMeUbiViewController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                    }
                }
            }
//            NSLog(@"%@/%@",ZZCURL_API_USERASSETS,token);
            //            self.count = responseObject[@"data"][@"count"];
            //            self.dueModel = [ZZCDue mj_objectWithKeyValues:responseObject[@"data"][@"list"]];
            //            [self.dueTableView reloadData];
        }
    }];

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (IBAction)existBtnClick:(id)sender {
//    NSArray *arr = @[@"3期",@"6期"];
    [self.menuView removeFromSuperview];
    self.menuView= [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.existView.frame), ZZCScreenW, 132) contentArray:_cycleArr];
    __weak typeof (ZZCMenuView *) weakSelf = self.menuView;
    __weak typeof (self) weakself = self;
    self.menuView.block = ^(BOOL isSeletct ,NSString *title){
        
        [weakSelf removeFromSuperview];
        if (title.length != 0) {
            [weakself.existBtn setTitle:title forState:0];
                NSInteger Index  = [weakself.cycleArr indexOfObject:title];
                weakself.iid = [weakself.idArr[Index] floatValue];
                weakself.rateLabel.text = weakself.rateArr[Index];
        }
    };
    
    [self.view addSubview:self.menuView];
}


@end
